package com.atjg.autocat.utils

import android.annotation.SuppressLint
import android.content.Context
import java.io.BufferedWriter
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.FileWriter
import java.io.IOException
import java.util.concurrent.ExecutionException


class LogUtils() {
    companion object {
        @SuppressLint("StaticFieldLeak")
        private var logUtils: LogUtils? = null
        @SuppressLint("StaticFieldLeak")
        private var file: File? = null
        fun initLog(mContext: Context): LogUtils {
            if (logUtils == null) {
                file = File("${mContext.externalCacheDir}/deliveryLog.txt")
                if (file?.exists() != true) {
                    file?.createNewFile()
                }
                logUtils =  LogUtils()
            }
            return logUtils!!
        }
    }

    fun input2File(input: String): Boolean {
        var bw: BufferedWriter? = null
        try {
            // 构造给定文件名的FileWriter对象，并使用布尔值指示是否追加写入的数据。
            val fileWriter = FileWriter(file, true)
            bw = BufferedWriter(fileWriter)
            bw.write(input)
            return true
        } catch (e: IOException) {
            e.printStackTrace()
            return false
        } finally {
            try {
                bw?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return false
    }
    fun clearFile(){
        file?.delete()
    }
}